package com.zhanghe.study.java8.lambda;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * 四个内置函数式接口
 */
public class TestLambda2 {

    /**
     * Consumer<T> 消费型接口
     * void accept(T t)
     */
    public static void testConsumer(){
        Consumer<Double> consumer = d -> System.out.println("花费"+d+"元");
        consumer.accept(1000d);
    }

    /**
     * Supplier<T> 供给型接口
     *  T get()
     */
    public static void testSupplier(){
        Supplier<Integer> supplier = () -> {
            return 100;
        };
        System.out.println(supplier.get());
    }

    /**
     * Function<T,R> 函数型接口
     * R apply(T t)
     */
    public static void testFunction(){
        Function<Integer,Integer> function = x -> {
            return x*100;
        };
        System.out.println(function.apply(10));
    }

    /**
     * Predicate<T> 断言型接口
     * boolean test(T t)
     */
    public static void testPredicate(){
        Predicate<String> predicate = x -> {
            return "yes".equals(x);
        };
        System.out.println(predicate.test("no"));
    }

    public static void main(String[] args) {
        testConsumer();
        testSupplier();
        testFunction();
        testPredicate();
    }
}
